''<!--******************************************************************************************-->
''
''  警告: 本電腦程式著作受著作權法及國際公約之保護。
''  未經授權擅自複製或散佈本電腦程式著作之全部或部分
''  ，將導致嚴厲的民事或刑事處份，且將被依法提起最大
''  範圍的追訴。
''
''  CopyRight (C) Galaxy Software Service
''<!--******************************************************************************************-->

''<!--******************************************************************************************-->
''函數名稱：GSSchangeDate
''目    的：檢查輸入日期資料是否正確
''參數說明：vvntObj：欲檢查起始的值，或欲檢查的起始HTML輸入項目「INPUT TYPE='TEXT'」
''			當為傳遞參數時，預設檢查觸發的事件的物件
''回傳值：True if valid, otherwise false.
''版本變更：
''xx. YYYY/MM/DD	AUTHOR		COMMENTS
'' 1. 2002/06/19	Brenda		New Create
''<!--******************************************************************************************-->
Function GSSchangeDate(vvntObj)
	Dim objDateBox
	Dim strDateBoxVal
	Dim strODate
	Dim strYear,strMonth,strDay
	Dim strCYear
	Dim strDate
	Dim strWeekName
	Dim datDate
	Dim i
	Dim arrTmp
	Dim blnClear
	Dim intDateType
	Dim strDATA_TYPE
	Dim strSign		'日期前面是否有負號

	IntDateType=document.all("GSSintDateType").value
	strDATA_TYPE = "DATE"
    
	'當傳入資料格式為字串時，如果該字串中含有\,時，透過split轉換為陣列
	'0:檢查的日期，1：轉換的日期格式
	If LCase(TypeName(vvntObj))="string" And vvntObj<>"" Then
		If InStr(vvntObj,"\,")>0 Then
			arrTmp = Split(vvntObj,"\,")
			strDateBoxVal = Replace(arrTmp(0)," ","")
			intDateType = arrTmp(1)
			If UBound(arrTmp)=2 Then
				strDATA_TYPE = arrTmp(2)
			End If
		Else
			strDateBoxVal = Replace(vvntObj," ","")
		End If
		blnClear=False
	ElseIf IsObject(vvntObj) Then 
		Set objDateBox = vvntObj
		If ((objDateBox.disabled) Or (objDateBox.readOnly)) Then
			GSSchangeDate=True
			Exit Function
		End If
		strDateBoxVal = objDateBox.value        
		blnClear=True
	Else
		Set objDateBox = window.event.srcElement
		strDateBoxVal = Replace(objDateBox.value," ","")
		blnClear=True
	End If
	If IsObject(objDateBox) Then 
		strDATA_TYPE=objDateBox.getAttribute("DATA_TYPE")
	End If

	If strDateBoxVal="" Then
		GSSchangeDate=False
		Exit Function
	End If

	
	'轉換日期為標準模式 "19970506"
	'XXXX-XX-XX
	strODate = strDateBoxVal

''	判斷是否為民國前，如果是strSign="-"
	If Left(strDateBoxVal, 1) = "-" Then
		strSign = "-"
		strDateBoxVal = Mid(strDateBoxVal, 2)
	Else
		strSign = ""
	End If
	
	If InStr(strDateBoxVal, "/") = 0 And InStr(strDateBoxVal, "-") = 0 And InStr(strDateBoxVal, ".") = 0 Then
	'可能為沒有分隔符號的日期格式（中文的AP通常允許，但是用 IsDate 來判斷會錯誤）
		For i = 1 To Len(strDateBoxVal)
			If InStr("0123456789",Mid(strDateBoxVal, i, 1))>0 Then
				strDate = strDate & Mid(strDateBoxVal, i, 1)   '若為數字則加入，剔除非數字的字元
			End If
		Next

		If ((strDATA_TYPE = "@YEARMONTH") And ((Len(strDate) < 4) Or (Len(strDate) > 6)) Or _
			(strDATA_TYPE = "DATE") And ((Len(strDate) < 6) Or (Len(strDate) > 8))) Then
			'日期不合法
		Else
			If (strDATA_TYPE = "DATE") Then		'DATE
				' 8 為西元
				If Len(strDate) < 8 Then
					strYear = Trim(CStr(CInt(Mid(strDate, 1, 2 + (Len(strDate) Mod 3)) + 1911)))
					strMonth = Mid(strDate, 3 + (Len(strDate) Mod 3), 2)
					strDay = Right(strDate, 2)
 				Else
					strYear = Mid(strDate, 1, 4)
					strMonth = Mid(strDate, 5, 2)
					strDay = Mid(strDate, 7, 2)
				End If
			Else		'@YEARMONTH
				If Len(strDate) < 6 Then
					strYear = Trim(CStr(CInt(Mid(strDate,1,Len(strDate)-2)) + 1911))
					strMonth = Right(strDate, 2)
				Else
					strYear = Mid(strDate, 1, 4)
					strMonth = Mid(strDate, 5, 2)
				End If
					strDay = "01"	
			End If
			strDateBoxVal = strYear & "/" & strMonth & "/" & strDay
		End If
	End If

	strDateBoxVal = Replace(strDateBoxVal,".", "/")
	strDateBoxVal = Replace(strDateBoxVal,"-", "/")

	If InStr(strDateBoxVal,"/")=0 Then
		Call GSSMsgDesc(10401,"")
		If blnClear Then Call GSSClearObj(objDateBox)
		GSSchangeDate=False
		Exit Function
	End If
''	strYear = DatePart("yyyy",strDateBoxVal)
	strYear = Mid(strDateBoxVal,1,InStr(strDateBoxVal,"/")-1)
	Dim strTmp 'As String 
	strTmp = strYear
	
	If (InStr(1, strDateBoxVal, strYear) = 0) Or (Len(strYear)<>4) Then
	'原字串找不到，為民國年
		strCYear = strYear
		''strCYear = DatePart("yyyy",strDateBoxVal)
		'判斷是否超過民國一百年
		If InStr(1, strDateBoxVal, "1" & strCYear) <> 0 Then
			strCYear = "1" & strCYear
		End If

	       '是否為民國前
	        If strSign = "-" Then
			strYear = CStr(1911 - CInt(strCYear))
	        Else
			strYear = CStr(CInt(strCYear) + 1911)
	        End If

	Else
		'西元年
		strCYear = CStr(CInt(strYear) - 1911)

		If Left(strCYear, 1) = "-" Then
			strCYear = Mid(strCYear, 2)
			strSign = "-"
		End If
	End If

	strDateBoxVal = Replace(strDateBoxVal,strTmp & "/", strYear & "/")

	If Not IsDate(strDateBoxVal) Then
		Call GSSMsgDesc(10401,"")
		If blnClear Then Call GSSClearObj(objDateBox)
		GSSchangeDate=False
		Exit Function
	End If

	strDate = CDate(strDateBoxVal)
	strMonth = Right("00" & DatePart("m",strDate),2)
	strDay = Right("00" & DatePart("d",strDate),2)
	datDate=CDate(strDate)

	'好了 我們現在有標準的日期格式了
	'找出星期幾
	strWeekName=GSSgetWeekName(datDate)
	If (intDateType=2) Or (intDateType=15) Then
		strCYear=Right("000" & strCYear,3)
	End If
	If (intDateType=17) Or (intDateType=18) Then
		strCYear = Right("   " & strCYear,3)
	End If
	Select Case intDateType
		Case 0   '1997/08/05'
		'strDate = strDate
		If strDATA_TYPE="@YEARMONTH" Then
			strDate = strYear & "/" & strMonth
		Else
			strDate = strYear & "/" & strMonth & "/" & strDay
		End If
	Case 1,2   '1 - "85/08/05" , 2 - "085/08/05" , 18 - " 86/08/05"
		If strDATA_TYPE="@YEARMONTH" Then
			strDate = strSign & strCYear & "/" & strMonth
		Else
			strDate = strSign & strCYear & "/" & strMonth & "/" & strDay
		End If
	Case 3   '1997年08月05日
		strDate = strYear & "年" & strMonth & "月" & strDay & "日"
	Case 4   '86年08月05日
		strDate = strSign & strCYear & "年" & strMonth & "月" & strDay & "日"
	Case 5   '八十六年八月五日
		strDate = NumToCNum(strCYear, False, False) & "年" & NumToCNum(strMonth, False, False) & "月" & NumToCNum(strDay, False, False) & "日"
		If(strSign = "-") Then strDate = "前" & strDate
	Case 6   '民國八十六年八月五日
		strDate = NumToCNum(strCYear, False, False) & "年" & NumToCNum(strMonth, False, False) & "月" & NumToCNum(strDay, False, False) & "日"
		If(strSign = "-") Then
			strDate = "民國前" & strDate
		Else
			strDate = "民國" & strDate
		End If
	Case 7   '中華民國八十六年八月五日
		strDate = NumToCNum(strCYear, False, False) & "年" & NumToCNum(strMonth, False, False) & "月" & NumToCNum(strDay, False, False) & "日"
		If(strSign = "-") Then
			strDate = "中華民國前" & strDate
		Else
			strDate = "中華民國" & strDate
		End If
	Case 8   '1997年08月05日星期一
		strDate = strYear & "年" & strMonth & "月" & strDay & "日" & strWeekName
	Case 9   '86年08月05日星期一
		strDate = strCYear  & "年" & strMonth & "月" & strDay & "日" & strWeekName
		If(strSign = "-") Then strDate = "前" & strDate
	Case 10   '八十六年八月五日星期一
		strDate = NumToCNum(strCYear, False, False) & "年" & NumToCNum(strMonth, False, False) & "月" & NumToCNum(strDay, False, False) & "日" & strWeekName
		If(strSign = "-") Then strDate = "前" & strDate
	Case 11   '民國八十六年八月五日星期一
		strDate = NumToCNum(strCYear, False, False) & "年" & NumToCNum(strMonth, False, False) & "月" & NumToCNum(strDay, False, False) & "日" & strWeekName
		If(strSign = "-") Then
			strDate = "民國前" & strDate
		Else
			strDate = "民國" & strDate
		End If
	Case 12   '中華民國八十六年八月五日星期一
		strDate = NumToCNum(strCYear, False, False) & "年" & NumToCNum(strMonth, False, False) & "月" & NumToCNum(strDay, False, False) & "日" & strWeekName
		If(strSign = "-") Then
			strDate = "中華民國前" & strDate
		Else
			strDate = "中華民國" & strDate
		End If
	Case 13   '19970805
		If strDATA_TYPE="@YEARMONTH" Then
			strDate = strYear & strMonth
		Else
			strDate = strYear & strMonth & strDay
		End If
	Case 14,15,17   '14 - "860805" , 15 - "0860805" , 17 - " 860805"
		If strDATA_TYPE="@YEARMONTH" Then
			strDate = strSign & strCYear & strMonth
		Else
			strDate = strSign & strCYear& strMonth & strDay
		End If
	Case 16   '星期一
		strDate = strWeekName
	Case Else
		strDate = "沒有指定輸出形態"
	End Select

	If IsObject(objDateBox) Then
		objDateBox.value=strDate
		GSSchangeDate=True
	Else
		GSSchangeDate = strDate
	End If    
End Function

''<!--******************************************************************************************-->
''函數名稱：GSSgetWeekName
''目    的：根據日期取得星期的中文
''參數說明：vdatDate：日期「格式必須是標準的格式」
''			當為傳遞參數時，預設檢查觸發的事件的物件
''回傳值：星期名稱字串
''備註：「此為內部使用函數，起勿使用」
''版本變更：
''xx. YYYY/MM/DD	AUTHOR		COMMENTS
'' 1. 2002/06/19	Brenda		New Create
''<!--******************************************************************************************-->
Function GSSgetWeekName(vdatDate)
    Dim intWeek
    
    intWeek=DatePart("w", vdatDate)
    If IsEmpty(GSSGetAPInfoMess) Then
        Select Case intWeek
            Case 1
                GSSgetWeekName = "星期日"
            Case 2
                GSSgetWeekName = "星期一"
            Case 3
                GSSgetWeekName = "星期二"
            Case 4
                GSSgetWeekName = "星期三"
            Case 5
                GSSgetWeekName = "星期四"
            Case 6
                GSSgetWeekName = "星期五"
            Case 7
                GSSgetWeekName = "星期六"
        End Select
    Else
        Select Case intWeek
            Case 1
                GSSgetWeekName = GSSGetAPInfoMess(10800)
            Case 2
                GSSgetWeekName = GSSGetAPInfoMess(10801)
            Case 3
                GSSgetWeekName = GSSGetAPInfoMess(10802)
            Case 4
                GSSgetWeekName = GSSGetAPInfoMess(10803)
            Case 5
                GSSgetWeekName = GSSGetAPInfoMess(10804)
            Case 6
                GSSgetWeekName = GSSGetAPInfoMess(10805)
            Case 7
                GSSgetWeekName = GSSGetAPInfoMess(10806)
        End Select    
    End If
End Function